package org.streets.eis.component.messagebox;

import org.apache.wicket.Component;
import org.apache.wicket.IClusterable;
import org.apache.wicket.IRequestTarget;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

public class MessageBox extends Panel{
	
	private static final long serialVersionUID = 1L;

	protected static final String MESSAGE_BOX_WICKET_ID = "MessageBox";

	protected static final String	DEFAULT_ALERT_TITLE = "提示信息";
	protected static final String	DEFAULT_QUESTION_TITLE = "是否...";
	protected static final String	DEFAULT_CONFIRM_TITLE = "确定...";
	
	protected static final int DEFAULT_WIDTH = 450;
	protected static final int DEFAULT_HEIGHT = 150;
	
	public enum IconType {
		INFO,
		QUESTION,
		WARNING,
		ERROR
	}

	public enum ButtonType {
		OK,
		YESNO,
		OKCANCEL,
	}
	
	public enum ReturnType {
		NONE,
		OK,
		YES,
		NO,
		CANCEL,
		CUSTOM,
	}
	
	public interface DialogCallback extends IClusterable{
		void onDialogClosed(AjaxRequestTarget target,ReturnType result);
	}
	
	protected ModalWindow modalWindow ;
	protected ReturnType returnType = ReturnType.NONE ;
	
	private Component focusedComponentAfterClosed;
	
	public MessageBox(String id){
		this(id,null);
	}
	
	public MessageBox(final String id, final IModel<?> model){
		super(id);
		setRenderBodyOnly(true);
		add(modalWindow = new ModalWindow(MESSAGE_BOX_WICKET_ID));
		modalWindow.setWidthUnit("px");
		modalWindow.setHeightUnit("px");
		modalWindow.setPageMapName("messagebox");
		modalWindow.setResizable(false);
		modalWindow.setCssClassName(ModalWindow.CSS_CLASS_GRAY);
	}
	
	public ModalWindow getModalWindow(){
		return modalWindow;
	}
	
	public void setReturnType(ReturnType type){
		returnType = type;
	}
	
	protected Page newBoxInnerPage(final String content,final IconType iconType,final ButtonType buttonType,final DialogCallback callback){
		return new BoxInnerPage(this,content,iconType,buttonType,callback);
	}
	
	public void show(final String title,final String content,final IconType iconType,final ButtonType buttonType,final DialogCallback callback,int width,int height){
		IRequestTarget requestTarget = getRequestCycle().getRequestTarget();
		if (requestTarget instanceof AjaxRequestTarget){
			AjaxRequestTarget target = (AjaxRequestTarget) requestTarget;
			
			modalWindow.setPageCreator(new PageCreator(){
				private static final long serialVersionUID = 1L;
				public Page createPage() {
					return newBoxInnerPage(content,iconType,buttonType,callback);
				}			
			});
				
			modalWindow.setTitle(title);
			modalWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback(){
				private static final long serialVersionUID = 1L;
				public void onClose(AjaxRequestTarget target){
					if (callback!=null){
						callback.onDialogClosed(target,returnType);
					}
					if (MessageBox.this.focusedComponentAfterClosed!=null){
						target.focusComponent(MessageBox.this.focusedComponentAfterClosed);
					}
				}
			});
			
			modalWindow.setCloseButtonCallback(new ModalWindow.CloseButtonCallback(){
				private static final long serialVersionUID = 1L;
				public boolean onCloseButtonClicked(AjaxRequestTarget target){
					if (callback!=null){
						callback.onDialogClosed(target,returnType);
					}
					if (MessageBox.this.focusedComponentAfterClosed!=null){
						target.focusComponent(MessageBox.this.focusedComponentAfterClosed);
					}
					return true;
				}
			});	
			
			
			width = width<=0?DEFAULT_WIDTH:width;
			height = height<=0?DEFAULT_HEIGHT:height;
			modalWindow.setMinimalWidth(width);
			modalWindow.setInitialWidth(width);
			modalWindow.setMinimalHeight(height);
			modalWindow.setInitialHeight(height);
			modalWindow.show(target);	
			
		} else{
			throw new RuntimeException("MessageBox must be used in an Ajax request.");
		}
		
	}
	public MessageBox setFocusedComponentAfterClosed(Component c){
		focusedComponentAfterClosed = c;
		return this;
	}
	
	public void alert(String content){
		alert(DEFAULT_ALERT_TITLE,content,null);
	}
	
	public void alert(String content,final DialogCallback callback){
		alert(DEFAULT_ALERT_TITLE,content,callback);
	}	
	
	public void alert(String content,int width,int height){
		alert(DEFAULT_ALERT_TITLE,content,null,width,height);
	}		

	public void alert(String content,final DialogCallback callback,int width,int height){
		alert(DEFAULT_ALERT_TITLE,content,callback,width,height);
	}
	
	public void alert(String title,String content){
		alert(title,content,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	public void alert(String title,String content,final DialogCallback callback){
		alert(title,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}	

	public void alert(String title,String content,int width,int height){
		alert(title,content,null,width,height);
	}
	
	public void alert(String title,String content,final DialogCallback callback,int width,int height){
		show(title,content,IconType.INFO,ButtonType.OK,callback,width,height);
	}
	
	public void error(String content){
		error(DEFAULT_ALERT_TITLE,content);
	}
	
	public void error(String content,final DialogCallback callback){
		error(DEFAULT_ALERT_TITLE,content,callback);
	}
	
	public void error(String content,int width,int height){
		error(DEFAULT_ALERT_TITLE,content,width,height);
	}	
	
	public void error(String content,final DialogCallback callback,int width,int height){
		error(DEFAULT_ALERT_TITLE,content,callback,width,height);
	}		
	
	public void error(String title,String content){
		error(title,content,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	public void error(String title,String content,final DialogCallback callback){
		error(title,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	public void error(String title,String content,int width,int height){
		error(title,content,null,width,height);
	}		
	
	public void error(String title,String content,final DialogCallback callback,int width,int height){
		show(title,content,IconType.ERROR,ButtonType.OK,callback,width,height);
	}		
	
	public void warning(String content){
		warning(DEFAULT_ALERT_TITLE,content);
	}
	
	public void warning(String content,final DialogCallback callback){
		warning(DEFAULT_ALERT_TITLE,content,callback);
	}	
	
	public void warning(String content,int width,int height){
		warning(DEFAULT_ALERT_TITLE,content,width,height);
	}

	public void warning(String content,final DialogCallback callback,int width,int height){
		warning(DEFAULT_ALERT_TITLE,content,callback,width,height);
	}

	public void warning(String title,String content){
		warning(title,content,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	public void warning(String title,String content,final DialogCallback callback){
		warning(title,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}	
	
	public void warning(String title,String content,int width,int height){
		warning(title,content,null,width,height);
	}	
	
	public void warning(String title,String content,final DialogCallback callback,int width,int height){
		show(title,content,IconType.WARNING,ButtonType.OK,callback,width,height);
	}		
	
	public void question(String content,final DialogCallback callback){
		question(DEFAULT_QUESTION_TITLE,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	public void question(String content,final DialogCallback callback,int width,int height){
		question(DEFAULT_QUESTION_TITLE,content,callback,width,height);
	}
	
	public void question(String title,String content,final DialogCallback callback){
		question(title,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}
	
	public void question(String title,String content,final DialogCallback callback,int width,int height){
		show(title,content,IconType.QUESTION,ButtonType.YESNO,callback,width,height);
	}
	
	public void confirm(String content,final DialogCallback callback){
		confirm(DEFAULT_CONFIRM_TITLE,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}	
	
	public void confirm(String content,final DialogCallback callback,int width,int height){
		confirm(DEFAULT_CONFIRM_TITLE,content,callback,width,height);
	}	
	
	public void confirm(String title,String content,final DialogCallback callback){
		confirm(title,content,callback,DEFAULT_WIDTH,DEFAULT_HEIGHT);
	}	
	
	public void confirm(String title,String content,final DialogCallback callback,int width,int height){
		show(title,content,IconType.WARNING,ButtonType.OKCANCEL,callback,width,height);
	}		
	
}
